local unread=0;
 
function XBarMail_OnEvent(this,event)
--	ChatFrame1:AddMessage("XBar-Mail : "..event,arg1);
	if (event=="LOADING_END") then
		this:RegisterEvent("MAIL_SHOW");
		this:RegisterEvent("CHAT_MSG_SYSTEM");
	end
	if (event=="MAIL_SHOW") then
		XBarMail_B_Icon:SetTexture("Interface\\Icons\\Quest_Letter08");
		unread=0;
	end
	if (event=="CHAT_MSG_SYSTEM") and (arg1==TEXT("SYS_NEW_MAIL")) then
		XBarMail_B_Icon:SetTexture("Interface\\Icons\\Quest_Letter07");
		unread=unread+1;
	end
--	Output
	local usrtxt={[1]=XBar["MailV1"],[2]=XBar["MailV2"]};
	local output="";
	for i=1,2 do
		usrtxt[i],_=XBar_StrReplace(usrtxt[i],"%[UNREAD%]",tostring(unread));
	end
	if (XBar["MailT1"]==true) then output=usrtxt[1]; end
	if (XBar["MailT2"]==true) then
		if (XBar["MailT1"]==true) then output=output.."\n"..usrtxt[2]; else output=usrtxt[2]; end
	end
	XBarMail_F_Text:SetText(output);
--	EoL
end

function XBarMail_OnClick()
	if (TimeLet_GetLetTime("MailLet")<0) then StaticPopup_Show("TIMEFLAG_FAIL1"); return
	else OpenMail(); end
end
